// pages/index/apply/rentApply.js
const app = getApp()
import api from '../../config/api'
import path from '../../config/path'
const { $Message } = require('../../dist/base/index');
const { $Toast } = require('../../dist/base/index');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    picLocal:app.globalData.picLocal,
    currentTab:'viewApply',
    viewApplyCount:0,
    rentApplyCount:0,
    viewCurrentTab:'viewWait',
    rentCurrentTab:'rentWait',
    waitViewApply:null,
    waitRentApply:null,
    visitedViewApply:null,
    visitedRentApply:null,
    pageData:{
      pageNo:1,
      pageSize:5,
      count:0
    },
    bottomLoading:false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    //查询默认的看车申请数据或租车申请数据
    if(this.data.currentTab=='viewApply'){
      let isHandle = 1;
      if(this.data.viewCurrentTab=='viewWait'){
        isHandle = 0;
      }
      let params = {
        isHandle:isHandle,
        pageNo:this.data.pageData.pageNo,
        pageSize:this.data.pageData.pageSize
      }
      api.post(path.path.findPersonalViewApply,params).then(res=>{
        //判断是待处理的数据还是已处理的数据
        this.data.pageData.count = res.data.list;
        if(isHandle==1){
          this.setData({
            visitedViewApply:res.data.list,
            pageData:this.data.pageData
          })
        }else{
          this.setData({
            waitViewApply:res.data.list,
            pageData:this.data.pageData
          })
        }
      }).catch(err=>{
        console.log(err)
      })
    }else{
      //查询租车申请的数据
      let isHandle = 1;
      //如果为待处理
      if(this.data.rentCurrentTab=='rentWait'){
        isHandle = 0;
      }
      let params = {
        isHandle:isHandle,
        pageNo:this.data.pageData.pageNo,
        pageSize:this.data.pageData.pageSize
      }
      api.post(path.path.findPersonalRentApply,params).then(res=>{
        //判断是待处理的数据还是已处理的数据
        this.data.pageData.count = res.data.list;
        if(isHandle==1){
          this.setData({
            visitedRentApply:res.data.list,
            pageData:this.data.pageData
          })
        }else{
          this.setData({
            waitRentApply:res.data.list,
            pageData:this.data.pageData
          })
        }
      }).catch(err=>{
        console.log(err)
      })
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.bottomStart();
    //将页数增加1
    //如果没有数据后
    if(this.data.pageData.count<(this.data.pageData.pageNo*this.data.pageData.pageSize)){
      $Toast({
        content:'没有更多数据了'
      })
      this.bottomEnd();
      return;
    }
    //页面触底之后加载第二页的数据加到第一页上去
    this.data.pageData.pageNo++;
    this.setData({
      pageData:this.data.pageData
    })
    //判断当前的数据是看车申请还是租车申请
    if(this.data.currentTab=='viewApply'){
      let isHandle = 1;
      if(this.data.viewCurrentTab=='viewWait'){
        isHandle = 0;
      }
      let params = {
        isHandle:isHandle,
        pageNo:this.data.pageData.pageNo,
        pageSize:this.data.pageData.pageSize
      }
      api.post(path.path.findPersonalViewApply,params).then(res=>{
        //判断是待处理的数据还是已处理的数据
        if(isHandle==1){
          this.data.pageData.count = res.data.count;
          let getList = res.data.list;
          let oraginList = this.data.visitedViewApply;
          getList.forEach((item)=>{
            oraginList.push(item)
          })
          this.setData({
            visitedViewApply:oraginList,
            pageData:this.data.pageData
          })
        }else{
          this.data.pageData.count = res.data.count;
          let getList = res.data.list;
          let oraginList = this.data.waitViewApply;
          getList.forEach((item)=>{
            oraginList.push(item)
          })
          this.setData({
            waitViewApply:oraginList,
            pageData:this.data.pageData
          })
        }
        this.bottomEnd();
      }).catch(err=>{
        console.log(err)
      })
    }else{
      //查询租车申请的数据
      let isHandle = 1;
      //如果为待处理
      if(this.data.rentCurrentTab=='rentWait'){
        isHandle = 0;
      }
      let params = {
        isHandle:isHandle,
        pageNo:this.data.pageData.pageNo,
        pageSize:this.data.pageData.pageSize
      }
      api.post(path.path.findPersonalRentApply,params).then(res=>{
        //判断是待处理的数据还是已处理的数据
        if(isHandle==1){
          this.data.pageData.count = res.data.count;
          let getList = res.data.list;
          let oraginList = this.data.visitedRentApply;
          getList.forEach((item)=>{
            oraginList.push(item)
          })
          this.setData({
            visitedRentApply:oraginList,
            pageData:this.data.pageData
          })
        }else{
          this.data.pageData.count = res.data.count;
          let getList = res.data.list;
          let oraginList = this.data.waitRentApply;
          getList.forEach((item)=>{
            oraginList.push(item)
          })
          this.setData({
            waitRentApply:oraginList,
            pageData:this.data.pageData
          })
        }
        this.bottomEnd();
      }).catch(err=>{
        console.log(err)
      })
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  
  applyChange({detail}){
    this.setData({
      currentTab:detail.key
    });
    this.data.pageData.pageNo = 1;
    //查询当前点击的标签的数据
    //如果当前点击的是看车申请
    if(this.data.currentTab=='viewApply'){
      //判断是查询未处理的数据还是处理过的数据
      let isHandle = 0;
      if(this.data.viewCurrentTab=='viewVisited'){
        isHandle = 1;
      }
      let params = {
        isHandle:isHandle,
        pageNo:this.data.pageData.pageNo,
        pageSize:this.data.pageData.pageSize
      }
      //向后端发送请求，查询数据
      api.post(path.path.findPersonalViewApply,params).then(res=>{
        this.data.pageData.count = res.data.count;
        if(isHandle==0){
          this.setData({
            waitViewApply:res.data.list,
            pageData:this.data.pageData
          })
        }else if(isHandle==1){
          this.setData({
            visitedViewApply:res.data.list,
            pageData:this.data.pageData
          })
        }
      }).catch(err=>{
        console.log(err)
      })
    }else if(this.data.currentTab=='rentApply'){
      //判断查询的是未处理的数据还是处理过的数据
      let isHandle = 0;
      if(this.data.rentCurrentTab=='rentVisited'){
        isHandle=1;
      }
      let params = {
        isHandle:isHandle,
        pageNo:this.data.pageData.pageNo,
        pageSize:this.data.pageData.pageSize
      }
      //向后端发送请求，查询数据
      api.post(path.path.findPersonalRentApply,params).then(res=>{
        this.data.pageData.count = res.data.count
        if(isHandle==0){
          this.setData({
            waitRentApply:res.data.list,
            pageData:this.data.pageData
          })
        }else if(isHandle==1){
          this.setData({
            visitedRentApply:res.data.list,
            pageData:this.data.pageData
          })
        }
      }).catch(err=>{
        console.log(err)
      })
    }
  },

  viewChange({detail}){
    this.setData({
      viewCurrentTab:detail.key
    })
    //如果切换为未处理，那么就查询未处理的数据
    this.data.pageData.pageNo = 1;
    if(this.data.viewCurrentTab=='viewWait'){
      let params = {
        pageNo:this.data.pageData.pageNo,
        pageSize:this.data.pageData.pageSize,
        isHandle:0
      }
      api.post(path.path.findPersonalViewApply,params).then(res=>{
        this.data.pageData.count = res.data.count;
        this.setData({
          waitViewApply:res.data.list,
          pageData:this.data.pageData
        })
      }).catch(err=>{
        console.log(err)
      })
    }
    //如果切换为已处理的数据，那么就查询已处理的数据
    if(this.data.viewCurrentTab=='viewVisited'){
      let params = {
        pageNo:this.data.pageData.pageNo,
        pageSize:this.data.pageData.pageSize,
        isHandle:1
      }
      api.post(path.path.findPersonalViewApply,params).then(res=>{
        this.data.pageData.count = res.data.count;
        this.setData({
          visitedViewApply:res.data.list,
          pageData:this.data.pageData
        })
      }).catch(err=>{
        console.log(err)
      })
    }
  },

  rentChange({detail}){
    this.setData({
      rentCurrentTab:detail.key
    })
    //判断查询的是未处理的数据还是处理过的数据
    let isHandle = 0;
    if(this.data.rentCurrentTab=='rentVisited'){
      isHandle=1;
    }
    this.data.pageData.pageNo = 1;
    let params = {
      isHandle:isHandle,
      pageNo:this.data.pageData.pageNo,
      pageSize:this.data.pageData.pageSize
    }
    //向后端发送请求，查询数据
    api.post(path.path.findPersonalRentApply,params).then(res=>{
      this.data.pageData.count = res.count;
      if(isHandle==0){
        this.setData({
          waitRentApply:res.data.list,
          pageData:this.data.pageData
        })
      }else if(isHandle==1){
        this.setData({
          visitedRentApply:res.data.list,
          pageData:this.data.pageData
        })
      }
    }).catch(err=>{
      console.log(err)
    })
  },

  toHandleViewApply(e){
    //跳转到处理看车申请的页面
    wx.navigateTo({
      url: './handle/handleViewApply?id='+e.currentTarget.dataset.id+"&isHandle="+e.currentTarget.dataset.ishandle,
    })
  },

  toHandleRentApply(e){
    //跳转到处理租车申请的页面
    wx.navigateTo({
      url: './handle/handleRentApply?id='+e.currentTarget.dataset.id+"&isHandle="+e.currentTarget.dataset.isHandle,
    })
  },

  //底部加载
  bottomStart(){
    this.setData({
      bottomLoading:true
    })
  },

  //底部不加载
  bottomEnd(){
    this.setData({
      bottomLoading:false
    })
  },
})